//
//  WXMainViewController.swift
//  WXWeiBo
//
//  Created by Angel-xin on 2016/09/19.
//  Copyright © 2016年 Angel-xin. All rights reserved.
//

import UIKit
import SVProgressHUD

class WXMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置 tintColor -> 统一设置tabbar的选中颜色
        UITabBar.appearance().tintColor = UIColor.orange
        
        let addButton = WXTabBar()
        
        // 设置代理对象
        addButton.wxDelegate = self
        
        // 在此使用self会产生循环引用, 解决办法使用[weak self]
        addButton.composeButtonClosure = { [weak self] in
            print("闭包")
            if !WXUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            
//            // 代码执行到此表示已经登录
//            let composeView = WXComposeView()
//            // 最上层的window
//            let window = UIApplication.shared.windows.last!
//            window.addSubview(composeView)
            if let target = self {
                // 代码执行到此表示已经登录
                let composeView = WXComposeView()
                composeView.show(target: target)
                
            }

        }
        
        setValue(addButton, forKey: "tabBar")
        

        // 两种方式添加子视图控制器
//        ViewController = []
//        addChildViewController(UIViewController)
        
        // 添加子视图控制器
        addChildViewController(childController: WXHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: WXMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: WXDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: WXProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    
    // 添加子视图控制器的方法重载
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        // 设置 tabbarItem 的图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        
        // 设置选中图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        
        // 设置标题(上,下)
        childController.title = title
        
        // 设置文字选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        // 设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
        
        // 创建导航视图控制器
        let nav = WXNavigationViewController(rootViewController: childController)
        
        // 添加子视图控制器
        addChildViewController(nav)
        
    }

}

// 使用extension可以实现代理方法
extension WXMainViewController: WXTabBarDelegate {
    
    func didSelectedComposeButton() {
        print("代理")
    }
    
}












